/**
 * 
 */
package br.srv.full.virtual.faces.manager.menu;

import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.faces.component.UIComponent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.component.UIPanelMenuGroup;
import org.richfaces.component.html.HtmlPanelMenuGroup;

import br.srv.full.virtual.entities.menu.MenuGroup;

/**
 * @author Delfino
 * 
 */
public class MapaAntiDuplicidade extends TreeMap<UIComponent, Set<UIComponent>> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4754967382833421825L;

	private final Log log = LogFactory.getLog(getClass());

	private UIPanelMenuGroup rootGroup = new HtmlPanelMenuGroup();
	private UIPanelMenuGroup allMenus = new HtmlPanelMenuGroup();

	{
		rootGroup.setId(MenuGroup.ROOT_GROUP_NAME);
		allMenus.setId("allMenuItens");
	}

	public MapaAntiDuplicidade() {
		super(new ComparadorUIComponent());
	}

	/**
	 * @param p_hpmg
	 * @return
	 */
	public boolean add(UIComponent p_hpmg) {
		return add(rootGroup, p_hpmg);
	}

	/**
	 * @param p_group
	 * @param p_menuItem
	 * @return
	 */
	boolean add(UIComponent p_group, UIComponent p_menuItem) {

		Set<UIComponent> l_set = get(p_group);
		if (l_set == null) {
			l_set = new TreeSet<UIComponent>(new ComparadorUIComponent());
			put(p_group, l_set);
		}
		boolean l_result = l_set.add(p_menuItem);
		log.info(p_menuItem.getId() + " @ " + p_group.getId() + " = " + l_result);

		if (!p_group.equals(allMenus))
			l_result &= add(allMenus, p_menuItem);

		return l_result;
	}
}
